package com.fenghen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fenghen.constans.RedisConstans;
import com.fenghen.dto.LoginDTO;
import com.fenghen.entity.Redis;
import com.fenghen.mapper.RedisMapper;
import com.fenghen.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl extends ServiceImpl<RedisMapper, Redis> implements RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisMapper redisMapper;

    @Override
    public String getcode(LoginDTO loginDTO) {
        //校验账号合理性
        checkall(loginDTO);
        String code = generateVerificationCode();
        String username = RedisConstans.Redis_User + loginDTO.getUsername();
        stringRedisTemplate.opsForValue().set(username, code, 5, TimeUnit.MINUTES);
        return code;
    }

    private String generateVerificationCode() {
        Random random = new Random();
        int code = 100000 + random.nextInt(900000); // 生成6位数的验证码
        return String.valueOf(code);
    }
    private void checkall(LoginDTO loginDTO) {
        if(loginDTO.getUsername() == null || loginDTO.getUsername().isEmpty()){
            throw new IllegalArgumentException("用户名不能为空");
        }
        if(loginDTO.getPasswordHash() == null || loginDTO.getPasswordHash().isEmpty()){
            throw new IllegalArgumentException("密码不能为空");
        }
        LambdaQueryWrapper<Redis> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(Redis::getUsername, loginDTO.getUsername());
        Redis redisuser = redisMapper.selectOne(queryWrapper);
        if(redisuser == null) {
            throw new IllegalArgumentException("用户名不存在");
        }
        if(!redisuser.getPasswordHash().equals(loginDTO.getPasswordHash())) {
            throw new IllegalArgumentException("密码错误");
        }
        if (redisuser.getIsDeleted() == 1) {
            throw new IllegalArgumentException("用户已被删除");
        }
        // 校验用户是否激活
        if (redisuser.getIsActive() == 0) {
            throw new IllegalArgumentException("用户未激活");
        }
    }

    @Override
    public void login(LoginDTO loginDTO) {
        checkall(loginDTO);
        if(loginDTO.getCode() == null || loginDTO.getCode().isEmpty()){
            throw new IllegalArgumentException("验证码不能为空");
        }
        if (loginDTO.getCode().length() != 6) {
            throw new IllegalArgumentException("必须为6位验证码");
        }
        String username = RedisConstans.Redis_User + loginDTO.getUsername();
        String chechcode = stringRedisTemplate.opsForValue().get(username);
        if(chechcode == null) {
            throw new IllegalArgumentException("验证码已过期或未发送");
        }
        if(!chechcode.equals(loginDTO.getCode())) {
            throw new IllegalArgumentException("验证码错误，请重试");
        }
    }
}
